/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyStatementExecution.MyInsert;

import MyDataContainer.MyDataSet;
import MyDataContainer.MyDataString;
import MyDataContainer.MyTable;
import MyDataType.MyInt;
import MyDataType.MyString;
import StringJoinerCutter.*;
import MyStatementExecution.MyStatement;
import MyStatementExecution.MyStatementKind;

/**
 *
 * @author Welcome
 */
public class MyInsertStatement extends MyStatement{

    
    private String tableName;
    
    private MyDataString listColumn;
    //private String insertSQL;
    public MyInsertStatement() {
        myStatementKind = MyStatementKind.INSERT;
        listColumn=new MyDataString();
        tableName="";
        
    }

    
    @Override
    public MyStatementKind getKind() {
        return myStatementKind;
    }

    @Override
    public void executeStatement() {
        try
        {
            String tempString = StringJoinerCutter.getStringInMidle(statementString, "into", "values").trim();
            if(tempString.indexOf("(")>-1)
            {
                tableName = tempString.substring(0,tempString.indexOf("(")).trim();
                
            }
            
            else
            {
                tableName = tempString.trim();
            }
            
            MyTable targetTable = databaseHandler.getTable(tableName);
            tempString = StringJoinerCutter.getStringInMidle(statementString, "values", "").trim();
            
            //Loai bo dau ()
            tempString = tempString.substring(1,tempString.length()-1);
            String[] datas = tempString.split(",");
            
            MyDataSet rowDataSet=new MyDataSet();
            MyDataString dataTypes = new MyDataString(targetTable.getDataTypeList());
            
            for(int i=0; i<dataTypes.getSize();i++)
            {
                if(dataTypes.getAt(i).equals("int"))
                {
                    MyInt myInt= new MyInt(datas[i]);
                    rowDataSet.addData(myInt);
                }
                if(dataTypes.getAt(i).equals("string"))
                {
                    MyString myString= new MyString(datas[i]);
                    rowDataSet.addData(myString);
                }
            }
            
            targetTable.addContent(rowDataSet);
            
            databaseHandler.WriteDatabase(databaseHandler.getPathFile());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
}
